#! /bin/bash

if [ -n "$2" ] ;
then
  do_date=$2
else
  do_date=`date -d '-1 day' +%F`
fi

env_config="
use dw;
SET mapreduce.job.queuename=tianyan;
SET hive.input.format=org.apache.hadoop.hive.ql.io.HiveInputFormat;"

dim_partition="
insert overwrite table dw.dim_partition partition (dt = '$do_date')
select business_type,
       from_unixtime(order_create_time, 'yyyy-MM-dd')
from ods_rebate_detail
where dt = '$do_date'
group by business_type,from_unixtime(order_create_time, 'yyyy-MM-dd');"

dim_rebate_record="
insert overwrite table dw.dim_rebate_record
select
       id,
       business_type,
       group_id,
       company_id,
       rebate_period_type,
       trade_type,
       consume_type,
       amount,
       actual_amount,
       rebate_amount,
       actual_rebate_amount,
       pid,
       source_id,
       cache_key,
       discount_id,
       period_id,
       rule_name,
       discount_type,
       discount_policy,
       update_flag,
       period_end_flag,
       recharge_end_flag,
       create_time,
       update_time
from ods_rebate_record orr
where orr.dt = '$do_date';"

dim_profit_cart="
insert overwrite table dw.dim_profit_cart

select si_id,
       p_id,
       p_number,
       p_user_id,
       p_user_name,
       p_brand_id,
       p_brand_name,
       p_name,
       cart_id,
       cart_number,
       cart_user_id,
       cart_user_name,
       cart_brand_id,
       cart_brand_name,
       cart_name
from
    (select
           si_id,
           p1.p_id,
           p1.p_number,
           p1.p_user_id,
           p1.p_user_name,
           p1.p_brand_id,
           p1.p_brand_name,
           p1.p_name,
           s1.cart_id,
           s1.cart_number,
           s1.cart_user_id,
           s1.cart_user_name,
           s1.cart_brand_id,
           s1.cart_brand_name,
           s1.cart_name
    from (
             select
                    si_id,
                    cart_id,
                    owtc1.cart_no  as cart_number,
                    owtc1.u_id     as cart_user_id,
                    owtu1.nickname as cart_user_name,
                    brand_id       as cart_brand_id,
                    owtsb1.name    as cart_brand_name,
                    cart_name      as cart_name,
                    parent_id
             from ods_wjy_tb_cart owtc1
                      left join ods_wjy_tb_user owtu1 on owtc1.u_id = owtu1.u_id and owtu1.dt = '$do_date'
                      left join ods_wjy_tb_station_brand owtsb1 on brand_id = owtsb1.id and owtsb1.dt = '$do_date'
             where level = 2 and owtc1.dt = '$do_date' and owtc1.status = 0
         ) s1
    left join
        (
            select
                   cart_id       as p_id,
                   owtc.cart_no  as p_number,
                   owtc.u_id     as p_user_id,
                   owtu.nickname as p_user_name,
                   brand_id      as p_brand_id,
                   owtsb.name    as p_brand_name,
                   cart_name     as p_name
            from ods_wjy_tb_cart owtc
                 left join ods_wjy_tb_user owtu on owtc.u_id = owtu.u_id and owtu.dt = '$do_date'
                 left join ods_wjy_tb_station_brand owtsb on brand_id = owtsb.id and owtsb.dt = '$do_date'
            where level = 1 and owtc.dt = '$do_date' and owtc.status = 0
    ) p1 on s1.parent_id = p1.p_id
        ) t2 where t2.p_id is not null;"

dim_profit_station="
insert overwrite table dw.dim_profit_station
select
       owtsi.si_id as station_id,
       owtsi.si_id as station_number,
       owtsi.name as station_name,
       owtsi.status as station_status,
       owtsi.brand_id as station_brand_id,
       owtsb.name as station_brand_name,
       owtsi.region_id as region_id,
       owtr.name as region_name,
       owtsi.web_type,
       owtsi.road_type,
       from_unixtime(owtsi.use_time)
from ods_wjy_tb_station_info owtsi
left join ods_wjy_tb_region owtr on owtr.region_id = owtsi.region_id and owtr.dt='$do_date'
left join ods_wjy_tb_station_brand owtsb on owtsi.brand_id = owtsb.id and owtsb.dt='$do_date'
where owtsi.dt='$do_date';"

dim_station_carts="
insert overwrite table dw.dim_station_carts
select dpc.si_id,
       concat_ws(',', collect_set(cast(p_id as string))) as p_ids,
       concat_ws(',', collect_set(cast(p_number as string))) as p_numbers,
       concat_ws(',', collect_set(cast(p_user_id as string))) as p_user_ids,
       concat_ws(',', collect_set(p_user_name)) as p_user_names,
       concat_ws(',', collect_set(cast(p_brand_id as string))) as p_brand_ids,
       concat_ws(',', collect_set(p_brand_name)) as p_brand_names,
       concat_ws(',', collect_set(p_name)) as p_names,
       concat_ws(',', collect_set(cast(cart_id as string))) as cart_ids,
       concat_ws(',', collect_set(cast(cart_number as string))) as cart_numbers,
       concat_ws(',', collect_set(cast(cart_user_id as string))) as cart_user_ids,
       concat_ws(',', collect_set(cart_user_name)) as cart_user_names,
       concat_ws(',', collect_set(cast(cart_brand_id as string))) as cart_brand_ids,
       concat_ws(',', collect_set(cart_brand_name)) as cart_brand_names,
       concat_ws(',', collect_set(cart_name)) as cart_names
from dim_profit_cart dpc
group by si_id;"


insert_into_table(){
  case $1 in
  "all")
    hive -e "$env_config$dim_partition$dim_profit_station$dim_profit_cart$dim_station_carts$dim_rebate_record"
    ;;
  "dim_partition")
    hive -e "$env_config$dim_partition"
    ;;
  "dim_profit_station")
    hive -e "$env_config$dim_profit_station"
    ;;
  "dim_profit_cart")
    hive -e "$env_config$dim_profit_cart$dim_station_carts"
    ;;
  "dim_rebate_record")
    hive -e "$env_config$dim_rebate_record"
    ;;
  esac
}

insert_into_table $1